package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.DanceKinds;
import com.example.demo.mapper.DanceKindsMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/danceKinds")
public class DanceKindsController {
    @Resource
    DanceKindsMapper danceKindsMapper;


    @PostMapping
    public Result<?> save(@RequestBody DanceKinds danceKinds) {

        danceKindsMapper.insert(danceKinds);
        return Result.success();

    }

    @PutMapping
    public Result<?> update(@RequestBody DanceKinds danceKinds) {
        danceKindsMapper.updateById(danceKinds);
        return Result.success();

    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        danceKindsMapper.deleteById(id);
        return Result.success();

    }

    @GetMapping("/Query")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {

        System.out.println("sss");

        LambdaQueryWrapper<DanceKinds> wrapper = Wrappers.<DanceKinds>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(DanceKinds::getId, search);
        }
        Page<DanceKinds> stuPage = danceKindsMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(stuPage);

    }
    @GetMapping("/wx")
    public Result<?> danceKinds() {

         List<DanceKinds> danceKindsList = danceKindsMapper.selectList(null);
//        System.out.println(danceKindsList);
         return Result.success(danceKindsList);

    }

    @PostMapping("/detail")
    public DanceKinds detail(@RequestBody JSONObject req) {
        String id = req.getStr("id");
        DanceKinds danceKinds =  danceKindsMapper.selectById(id);
        return danceKinds;


    }

}
